package com.huitian.net.utils

object GpsUtil {
  /**
   * 将火星座标系转为高德座标系
   * @param lon 经度
   * @param lat 纬度
   * @return (Double, Double) 元组
   */
  def getAdjustedLocation(lon: Double, lat: Double): (Double, Double) = {
    if (lon > 73.66 && lon < 135.05 && lat > 3.86 && lat < 53.55) {
      val pi = 3.1415926535897932384626
      val a = 6378245.0
      val ee = 0.00669342162296594323

      val x = lon - 105.0
      val y = lat - 35.0
      var dLat = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x))
      dLat += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0
      dLat += (20.0 * Math.sin(y * pi) + 40.0 * Math.sin(y / 3.0 * pi)) * 2.0 / 3.0
      dLat += (160.0 * Math.sin(y / 12.0 * pi) + 320 * Math.sin(y * pi / 30.0)) * 2.0 / 3.0
      var dLon = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x))
      dLon += (20.0 * Math.sin(6.0 * x * pi) + 20.0 * Math.sin(2.0 * x * pi)) * 2.0 / 3.0
      dLon += (20.0 * Math.sin(x * pi) + 40.0 * Math.sin(x / 3.0 * pi)) * 2.0 / 3.0
      dLon += (150.0 * Math.sin(x / 12.0 * pi) + 300.0 * Math.sin(x / 30.0 * pi)) * 2.0 / 3.0
      val radLat = lat / 180.0 * pi
      var magic = Math.sin(radLat)
      magic = 1 - ee * magic * magic
      val sqrtMagic = Math.sqrt(magic)
      dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi)
      dLon = (dLon * 180.0) / (a / sqrtMagic * Math.cos(radLat) * pi)

      val mgLon = lon + dLon //经度
      val mgLat = lat + dLat //纬度
      (mgLon, mgLat)
    } else {
      (0.0, 0.0)
    }
  }

}
